﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using RetrieveEntityDetails.CrmDiscoveryServiceProxy;
using Microsoft.Crm.Passport.Sample;

//http://technet.microsoft.com/en-us/library/ms914670.aspx

namespace RetrieveEntityDetails.Utility
{
    class CRMDiscoveryServiceHelper
    {
        CrmDiscoveryService oCrmDiscoveryService;
        public string Username;
        public string Password;
        public string Domain;
        public string ServerName;
        public string PortNumber;
        public bool IsIFD = false;
        public bool IsHTTPS = false;
        public bool IsCRMOnline = false;

        public CRMDiscoveryServiceHelper()
        {

        }

        public OrganizationDetail[] GetOrganizationList(out string PassportTicket)
        {
            oCrmDiscoveryService = new CrmDiscoveryService();
            if (!string.IsNullOrEmpty(PortNumber))
                oCrmDiscoveryService.Url = "http://" + ServerName + "/MSCRMServices/2007/{AuthType}/CrmDiscoveryService.asmx";
            else
                oCrmDiscoveryService.Url = "http://" + ServerName + ":" + PortNumber + "/MSCRMServices/2007/{AuthType}/CrmDiscoveryService.asmx";

            if (IsCRMOnline)
                oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("{AuthType}", "PASSPORT");

            if (IsIFD)
                oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("{AuthType}", "SPLA");
            else
            {
                oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("{AuthType}", "AD");
            }
            if (IsHTTPS)
                oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("http", "https");

            if (Username == null & Password == null & Domain == null)
            {
                oCrmDiscoveryService.UseDefaultCredentials = true;
            }
            if (!IsCRMOnline)
            {
                oCrmDiscoveryService.UseDefaultCredentials = false;
                oCrmDiscoveryService.Credentials = new NetworkCredential(Username, Password, Domain);
            }
            oCrmDiscoveryService.PreAuthenticate = true;
            RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
            if (IsCRMOnline)
            {
                RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
                RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)oCrmDiscoveryService.Execute(policyRequest);
                string partner = "crm.dynamics.com";
                string environment = "Production";
                LogonManager lm = new LogonManager();
                string password = string.Empty;
                password = Console.ReadLine();
                string passportTicket = lm.Logon(Username, Password, partner, policyResponse.Policy, environment);
                lm.Dispose();
                orgRequest.PassportTicket = passportTicket;
                PassportTicket = passportTicket;
            }
            else
            {
                orgRequest.UserId = Domain + "\\" + Username;
                orgRequest.Password = Password;
                PassportTicket = string.Empty;
            }
            RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)oCrmDiscoveryService.Execute(orgRequest);            
            return orgResponse.OrganizationDetails;
        }

    }
}
